package com.itheima.easy.service.impl;


import com.itheima.easy.dto.FloorDto;
import com.itheima.easy.service.FloorService;
import com.itheima.easy.service.RoomService;
import com.itheima.easy.entity.Floor;
import com.itheima.easy.mapper.FloorMapper;
import com.itheima.easy.service.BedService;
import com.itheima.easy.vo.BedVo;
import com.itheima.easy.vo.FloorVo;
import com.itheima.easy.vo.RoomVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;


@Service

public class FloorServiceImpl implements FloorService {


    @Autowired
    RoomService roomService;
    @Autowired
    BedService bedService;
    @Autowired
    private FloorMapper floorMapper;

    //增加楼层
    @Override
    public int addFloor(FloorDto floorDto) {
        Floor floor = new Floor();
        BeanUtils.copyProperties(floorDto, floor);
        return floorMapper.insert(floor);
    }



    //通过ID删除楼层
    @Override
    public int deleteFloor(Long id) {
        return floorMapper.deleteById(id);
    }



    //更新楼层信息
    @Override
    public int updateFloor(FloorDto floorDto) {
        Floor floor = new Floor();
        BeanUtils.copyProperties(floorDto, floor);
        floor.setUpdateTime(LocalDateTime.now());
        return floorMapper.updateById(floor);
    }



    //通过ID查询楼层信息
    @Override
    public FloorVo getFloor(Long id) {
        Floor floor = floorMapper.selectById(id);
        FloorVo floorVo = new FloorVo();
        BeanUtils.copyProperties(floor, floorVo);
        List<RoomVo> roomsByFloorId = roomService.getRoomsByFloorId(floor.getId());
        List<RoomVo> roomVos = roomsByFloorId.stream().map(roomVo -> {
            List<BedVo> bedsByRoomId = bedService.getBedsByRoomId(roomVo.getId());
            roomVo.setBedVoList(bedsByRoomId);
            return roomVo;
        }).collect(Collectors.toList());
        floorVo.setRoomVoList(roomVos);
        return floorVo;
    }



    //获取所有的楼层信息
    @Override
    public List<FloorVo> getAllFloors() {
        List<Floor> floors = floorMapper.selectAll();
        return floors.stream().map(floor -> {
            FloorVo floorVo = new FloorVo();
            BeanUtils.copyProperties(floor, floorVo);
            List<RoomVo> roomsByFloorId = roomService.getRoomsByFloorId(floor.getId());
            floorVo.setRoomVoList(roomsByFloorId);
            return floorVo;
        }).collect(Collectors.toList());
    }
}

